<?php
class DATABASEFUNCTIONS extends CONNECT{
	
	public function filterData($data){
		
		$filtered_data = "";
		
		$this->connectOpen();
		if(is_array($data)){
			foreach ($data as $key=>$value){
				
				if(is_array($data[$key])){
					$data[$key] = $this->filterData($data[$key]);
				}
				
				if(is_string($data[$key])){
					$data[$key] = trim(mysql_real_escape_string($data[$key]));
				}
			}
		}
		
		if(is_string($data)){
			$data = trim(mysql_real_escape_string($data));
		}
		$this->connectionClose();
		return $data;
	}
						
	public function filterForTableData($table,$dataarray){
		
		$column_filter_data = array();
		$table_columns = $this->fetchTableColumns($table, "");
		if(is_array($dataarray)){
			foreach($dataarray as $key=>$value){
				if(in_array($key, $table_columns)){
					$column_filter_data[$key] = $value;
				}
			}
		}
		return $column_filter_data; 
	}
	
	public function fetchTableColumns($table,$condition){
		
		$columns = array();
		
		$this->connectOpen();
		if(isset($table) && $table!=""){
			$condition = trim($condition);
			if(isset($condition) && $condition!=""){
				$sql = "show columns from $table $condition";	
			}else{
				$sql = "show columns from $table";
			}
			$res = mysql_query($sql);
			while($rows = mysql_fetch_assoc($res)){
				$columns[] = $rows['Field'];
			}
		}else{
			echo "Invalid Table Name";
		}
		$this->connectionClose();
		return $columns;
	}
	
	public function generateInsertArray($table,$dataarray){
		
		$table_filtered_data = $this->filterForTableData($table,$dataarray);
		$filter_data = $this->filterData($table_filtered_data);
		
		return $filter_data;
	}
}
?>